package com.datamation.swadeshi.view;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.SetTitleAdapter;

public class FragmentSalesManagementVanSales extends Fragment implements TabListener {
	View view;
	private ActionBar actionBar;
	
	FragmentSalesManagementVanSaleCustomer tab1;
	FragmentSalesManagementVanSalesHeader tab2;
	FragmentSalesManagementVanSalesOrderDetails tab3;
	FragmentSalesManagementVanSalesOthers tab4;
	FragmentSalesManagementVanSalesSummary tab5;

	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.sales_management_pre_sales, container, false);
		
		setHasOptionsMenu(true);
		
		actionBar = getActivity().getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
        //set title
		SetTitleAdapter objTitle =new SetTitleAdapter();
		objTitle.createCutomActionBarTitle("Van Sale - New Invoice", getActivity());
		
		MainActivity activity = (MainActivity) getActivity();
				
		//adding tabs
		Tab aTab1 = actionBar.newTab();
		aTab1.setText("Customer"); 
		aTab1.setTabListener(this);
        actionBar.addTab(aTab1);
        
        Tab aTab2 = actionBar.newTab();
        aTab2.setText("Order Header (SA)");
        aTab2.setTabListener(this);
        actionBar.addTab(aTab2);
        
        Tab aTab3 = actionBar.newTab();
        aTab3.setText("Order Details (SA)");
        aTab3.setTabListener(this);
        actionBar.addTab(aTab3);
          
        Tab aTab4 = actionBar.newTab();
        aTab4.setText("UR / MR / FI");
        aTab4.setTabListener(this);
        actionBar.addTab(aTab4);
        
        Tab aTab5 = actionBar.newTab();
        aTab5.setText("Summary");
        aTab5.setTabListener(this);
        actionBar.addTab(aTab5);
        
		//back button disable 
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.setOnKeyListener(new View.OnKeyListener() {
		        @Override
		        public boolean onKey(View v, int keyCode, KeyEvent event) {
		            Log.i("", "keyCode: " + keyCode);
		            if( keyCode == KeyEvent.KEYCODE_BACK ) {
		            	Toast.makeText(getActivity(),"Back Navigationid is disable", Toast.LENGTH_SHORT).show();
		                //getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		                return true;
		            }else if ((keyCode == KeyEvent.KEYCODE_HOME)) {
		    			
		    			getActivity().finish();
		    			
		    			return true;

		    		} else {
		                return false;
		            }
		        }
		    });
		
	
		return view;
	}

	
	

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		int selectedTabIndex = tab.getPosition();
		if (selectedTabIndex == 0) 
		{
			
			tab1 = new FragmentSalesManagementVanSaleCustomer();
			ft.replace(R.id.tabPlaceHolderInvoices, tab1);
		}
		else if (selectedTabIndex == 1) 
		{
			tab2 = new FragmentSalesManagementVanSalesHeader();
			ft.replace(R.id.tabPlaceHolderInvoices, tab2);
		}
		else if (selectedTabIndex == 3) 
		{
			tab4 = new FragmentSalesManagementVanSalesOthers();
			ft.replace(R.id.tabPlaceHolderInvoices, tab4);
		}
		else if (selectedTabIndex == 2)
		{
			tab3 = new FragmentSalesManagementVanSalesOrderDetails();
			ft.replace(R.id.tabPlaceHolderInvoices, tab3);
		}
		else if (selectedTabIndex == 4) 
		{
			tab5 = new FragmentSalesManagementVanSalesSummary();
			ft.replace(R.id.tabPlaceHolderInvoices, tab5);
		}

		
	}


	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		for(int i = 0; i < menu.size(); ++i){
			menu.removeItem(menu.getItem(i).getItemId());
		}
		//getActivity().getActionBar().setTitle("New Invoice");
		super.onCreateOptionsMenu(menu, inflater);
	}
	
//    //Title name method
//    private void createCutomActionBarTitle(String title){
//        
//    	getActivity().getActionBar().setDisplayShowCustomEnabled(true);
//    	getActivity().getActionBar().setDisplayShowTitleEnabled(false);
//
//        LayoutInflater inflator = LayoutInflater.from(getActivity());
//        View v = inflator.inflate(R.layout.custom_action_bar, null);
//
//        //Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/fat_tats.ttf");
//        Typeface tf = Typeface.createFromAsset(getActivity().getAssets(),"fonts/coopbl.ttf");
//        
//        TextView tvTitle = (TextView)v.findViewById(R.id.titleFragment1);
//        tvTitle.setText(title);
//        tvTitle.setTypeface(tf);
//      
//        //assign the view to the actionbar
//        getActivity().getActionBar().setCustomView(v);
//    }
}
